<template>
  <div>
    <p>
      <span>全选</span>
      <input type="checkbox" v-model="isAll" />
      <button @click="fanBtn">反选</button>
    </p>
    <ul>
      <li v-for="(obj, index) in msg" :key="index">
        <input type="checkbox" v-model="obj.checked" />
        <span>{{ obj.name }}</span>
      </li>
    </ul>
  </div>
</template>

<script>
export default {
  data() {
    return {
      msg: [
        {
          name: "猪八戒",
          checked: false,
        },
        {
          name: "孙悟空",
          checked: false,
        },
        {
          name: "唐僧肉",
          checked: false,
        },
        {
          name: "沙悟净",
          checked: false,
        },
      ],
    };
  },
  computed: {
    isAll: {
      set(val) {
        this.msg.forEach((obj) => (obj.checked = val));
      },
      get() {
        // every是数组的方法
        // 作用遍历数组每一项 找到第一个不符合条件的
        // every直接原地返回false
        return this.msg.every((obj) => obj.checked);
      },
    },
  },
  methods: {
    fanBtn() {
      this.msg.forEach((obj) => (obj.checked = !obj.checked));
    },
  },
};
</script>

<style>
</style>